package com.nwork.qingmanhua.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.util.LruCache;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nwork.qingmanhua.R;

import java.io.IOException;
import java.util.List;

/**
 * 引用图片的Adapter，用于加载item布局
 */
public class CategoryImageAdapter extends RecyclerView.Adapter<CategoryImageAdapter.ViewHolder> implements View.OnTouchListener {
    private Context context;
    private List<String> list;
    private View.OnClickListener listener;
    private LruCache<String, Bitmap> cache;

    public CategoryImageAdapter (Context context, List<String> list) {
        this.context = context;
        this.list = list;
        cache = new LruCache<String, Bitmap>(10);
        if (context instanceof View.OnClickListener){
            listener = (View.OnClickListener) context;
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_fragment_category, viewGroup, false);
        view.setOnTouchListener(this);
        if (listener != null) {
            view.setOnClickListener(listener);
        }
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        Bitmap bitmap = cache.get(list.get(i));
        if (bitmap == null) {
            try {
                bitmap = BitmapFactory.decodeStream(context.getAssets().open(list.get(i)));
                cache.put(list.get(i), bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Palette palette = Palette.generate(bitmap);
        viewHolder.image.setImageBitmap(bitmap);
        viewHolder.text.setText(list.get(i));
        Palette.Swatch swatch = palette.getVibrantSwatch();
        /*switch (i){
            case 0:
                //有活力的
                swatch = palette.getVibrantSwatch();
                break;
            case 1:
                //亮色有活力的
                swatch = palette.getLightVibrantSwatch();
                break;
            case 2:
                //暗色有活力的
                swatch = palette.getDarkVibrantSwatch();
                break;
            case 3:
                //柔和的
                swatch = palette.getMutedSwatch();
                break;
            case 4:
                //亮色柔和的
                swatch = palette.getLightMutedSwatch();
                break;
            case 5:
                //暗色柔和的
                swatch = palette.getDarkMutedSwatch();
                break;
        }*/
        if (swatch != null) {
            ((CardView) viewHolder.itemView).setCardBackgroundColor(swatch.getRgb());
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        CardView card = (CardView) v;
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                card.setCardElevation(context.getResources().getDimensionPixelSize(R.dimen.card_elevation_pass));
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                card.setCardElevation(context.getResources().getDimensionPixelSize(R.dimen.card_elevation));
                break;
        }
        //当CardView有OnClick事件时返回false,没有时返回true
        return listener == null;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView image;
        private TextView text;
        public ViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.image_fragment_category);
            text = (TextView) itemView.findViewById(R.id.text_fragment_category);
        }
    }
}
